package com.jie.deletefile.Imgeview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jie.deletefile.R;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class VpActivity extends AppCompatActivity {

    private android.widget.RelativeLayout activityvp;
    private List<String> data;
    private List<View> views;
    private MyVpAdapter adapter;
    private android.support.v4.view.ViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vp);
        this.vp = (ViewPager) findViewById(R.id.vp);
        this.activityvp = (RelativeLayout) findViewById(R.id.activity_vp);
        data = new ArrayList<>();
        views = new ArrayList<>();
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/44621585.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/41331577.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/98887668.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/14976634.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/65897578.jpg");
        for (int i = 0;i<data.size();i++) {
            View view = LayoutInflater.from(VpActivity.this).inflate(R.layout.item_vp, null);
            ImageView textView = (ImageView) view.findViewById(R.id.item_myimage);
            x.image().bind(textView, data.get(i));
            views.add(view);
        }
        adapter = new MyVpAdapter();
        vp.setAdapter(adapter);
        Intent intent = getIntent();
        int position = intent.getIntExtra("position", 0);
        vp.setCurrentItem(position);

    }


    class MyVpAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));

            return views.get(position);
        }
    }
}

